<?php
namespace server\script;
include 'cli.php';
use eqphp\db, server\table;

while (true) {
    execute();
    sleep(10);
}


function execute(){
    $field = 'id,operation,param,times';
    $expire = date('Y-m-d H:i:s', strtotime('-2 hours'));
    $condition['is_finish'] = 0;
    $condition['create_time'] = ['gt', $expire];
    $task_list = query(table::JOB_QUEUE)->select($field)->where($condition)->out('batch');
    if ($task_list && is_array($task_list)) {
        foreach ($task_list as $task) {
            $param = unserialize($task['param']);
            $action = 'server\job::' . $task['operation'];
            if (call_user_func_array($action, $param) || $task['times'] == 2) {
                $data['is_finish'] = 1;
            }
            $data['times'] = $task['times'] + 1;
            db::patch(table::JOB_QUEUE, $data, $task['id']);
            sleep(1);
        }
    }
}